# -*- coding: utf-8 -*-
class SearchEngine
  class << self

    def search_organs value
      return [] if value.blank?
      organ = Organ.search_by_fullname(value)

      value = value.downcase.split('').join("%").insert(0, "%").insert(-1, "%")

      ([organ] + Organ.where { (name.like value) | (pinyin.like value) }).uniq.compact
    end

    def search_users value, visible_leader
      origin = value.downcase.insert(0, "%").insert(-1, "%")
      value = value.downcase.split('').join("%").insert(0, "%").insert(-1, "%")

      if visible_leader == 1
        User.where { (name.like value) | (pinyin.like value) | (mobile_phone.like origin) | (office_address.like origin) | (office_phone.like origin) }
      else
        User.joins { members.organ }.where { (name.like value) | (pinyin.like value) | (mobile_phone.like origin) | (office_address.like origin) | (office_phone.like origin) }
            .where { (members.organ.is_leader == visible_leader) }
      end
    end

    def search_users_with_organ organ, visible_leader, page_num
      if visible_leader == 1
        User.joins { members.organ }.where { (members.organ.id == organ.id) }
            .order("sort DESC").paginate(:per_page => System.page_num, :page => page_num)
      else
        User.joins { members.organ }.where { (members.organ.id == organ.id) & (members.organ.is_leader == visible_leader) }
            .order("sort DESC").paginate(:per_page => System.page_num, :page => page_num)
      end
    end

  end
end
